<template>
    <div class="box">
        <header class="header">
            <van-nav-bar title="详情" left-arrow @click-left="$router.back()">
                <template #right>
                    <van-icon name="search" size="18" />
                </template>
            </van-nav-bar>
        </header>
        <main class="content">
            <div>
                <img style="width:100%" :src="detailInfo.image" alt="">
            </div>
            <div>{{ detailInfo.title }}</div>
            <div style="color:red"> ￥{{ detailInfo.price }}</div>
        </main>
        <footer class="footer">
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" dot />
            <van-action-bar-icon icon="cart-o" text="购物车" :badge="store.totalCount" @click="$router.push('/cart')"/>
            <van-action-bar-icon icon="shop-o" text="店铺" />
            <van-action-bar-button type="warning" text="加入购物车" @click="addCart" />
            <van-action-bar-button type="danger" text="立即购买" />
        </van-action-bar>
    </footer>
    </div>
    
</template>

<script setup>
import request from '@/utils/request'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {useCounterStore} from '@/stores/counter'
const store = useCounterStore()
const detailInfo = ref({})
const route = useRoute()
const router = useRouter()
onMounted(() => {
    request.get('/detail', {
        params: {
            id: route.params.id
        }
    }).then(res => {
        if (res.data.code == 200) {
            detailInfo.value = res.data.data
        }
    })
})
const addCart = () => {
    // 判断用户是否登录 -- localStorage中是否有token
    if (localStorage.getItem('token')) {
        // 说明用户已经登录，就将该商品加入购物车
     store.addItem(detailInfo.value)
    } else {
        // 说明用户未登录，跳转到登录页面
        router.push('/login')
    }
}
</script>